<template>
	<view class="index_2">
		<view class="search align-center justify-center flex">
			<view class="top-search flex align-center justify-between">
				<input placeholder="请输入关键字搜索" placeholder-class="phcolor" confirm-type="search"
					v-on:click="toInputSearch" @confirm="toInputResult" v-model="searchKey" />
				<text class="lg text-gray cuIcon-search"></text>
			</view>
			<text class="search_btn" @click="toInputResult">搜索</text>
		</view>

		<!--优惠专区-->
		<view class="discount" v-if="viewyh">
			<view class="discount_top">
				<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
					<view class="scroll-view-item_H" v-for="(tab,index) in GoodsCat" :key="tab.id" :id="tab.id"
						@click="checkIndex(index)">
						<view :class="navIndex==index ? 'text-bold text-black' : 'text-bold'">{{tab.title}}</view>
						<view :class="navIndex==index ? 'activite' : ''"></view>
					</view>
				</scroll-view>
			</view>
		</view>

		<view class="content" v-if="viewyh">
			<view class="discountlist">
				<block v-for="(item,index) in redGoodsList" v-key="index">
					<view class="recommend-item flex flex-direction discountitem" @tap="toGoodsDetail(item)">
						<image class="recommend-img1" :src="item.img_url" mode="widthFix" lazy-load="true"></image>
						<view class="recommend-info">
							<text class="goods-name">{{item.title}}</text>
							<view class="recommend-bto flex align-center justify-between">
								<view>
									<text class="text-price">{{item.price}}{{kongge}}</text>
									<text class="text-mkprice">{{item.original_prici}}</text>
									<text class="text-salenum">{{kongge}}销量 {{item.sales_count}}</text>
								</view>
								<view class="flex align-center justify-center">
									<image src="@/static/plus_2.png" mode="aspectFit" style="width:68rpx;height:68rpx;">
									</image>
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	var api = require('@/components/api.js');
	export default {
		name: "index_2",
		props: {

		},
		data() {
			return {
				refreshing: false,
				title: 'Hello',
				navIndex: 0,

				redGoodsList: [],
				activeGoodsList: [],
				dyadroollist: [],
				searchKey: '',
				selectList: [],
				hotGoodsList: [],
				GoodsCat: [],
				viewhot: 0,
				viewred: 0,
				viewyh: 1,
				kongge: ' ',
				category_id: 0
			}
		},
		created() {
			this.getGoodsList()
			this.loadGoodsCategory()
		},
		onReachBottom() {
			console.log('滑动到页面底部')
		},
		onPullDownRefresh() {
			console.log('下拉刷新');
			this.refreshing = true;

			this.getGoodsList()
			//this.loadSwiper()
		},
		methods: {
			checkIndex(index) {
				console.log(index)
				this.navIndex = index;
				let cate = this.GoodsCat[index]
				if (cate.id != 'tuijian') {
					this.category_id = cate.id;
				} else {
					this.category_id = 0;
				}
				this.redGoodsList = []
				this.getGoodsList()
			},
			scroll: function(e) {
				console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			loadGoodsCategory() {
				let that = this
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Website/GetAllGoodsCategoryList',
					data: {},
					success: data => {
						uni.hideLoading()
						console.log('sssss', data);
						if (data.ErrCode == 0) {
							let arr = [{
								title: '推荐',
								id: 'tuijian'
							}];
							if (data.Response.length > 0) {
								data.Response.forEach((item) => {
									arr.push(item)
								})
							}
							that.GoodsCat = arr

						}
					}
				});
				console.log('that.GoodsCat', that.GoodsCat);
			},
			getRedGoodsSys: function() {
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Website/GetsysRed',
					data: {},
					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.viewred = data.Response[0].sys_red
						}
					}
				});
			},
			getHotGoodsSys: function() {
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Website/GetsysHot',
					data: {},
					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.viewhot = data.Response[0].sys_hot
						}
					}
				});
			},
			getsysActive: function() {
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Website/GetsysActive',
					data: {},
					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.viewyh = data.Response[0].sys_active
						}
					}
				});
			},
			/*
			 * 加载推荐商品
			 */
			recgoodslist: function() {

				uni.showLoading({
					title: '加载中...'
				})

				api.get({
					url: 'Website/GetRedMoreGoods',

					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.redGoodsList = data.Response
						}
						this.refreshing = false;
					}
				});
			},
			/**
			 * 加载新品推荐
			 */
			getGoodsList: function() {
				uni.showLoading({
					title: '加载中...'
				})
				let postdata = {
					page: 1,
					pagesize: 15,
					category_id: this.category_id
				};
				api.get({
					url: 'Website/GetRedGoodsList',
					data: postdata,
					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.redGoodsList = data.Response
						}
						this.refreshing = false;
					}
				});
			},
			/**
			 * 加载优惠专区
			 */
			loadActiveGoodsList: function() {
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Goods/GetActiveGoodsList',
					data: {
						page: 1,
						pagesize: 15
					},
					success: data => {
						uni.hideLoading()
						console.log(data);
						if (data.ErrCode == 0) {
							this.activeGoodsList = data.Response
						}
					}
				});
			},
			/**
			 * 加载优惠专区
			 */
			loadSwiper: function() {
				api.get({
					url: 'Website/GetindexList',
					data: {
						page: 1,
						pagesize: 15
					},
					success: data => {
						console.log(data);
						if (data.ErrCode == 0) {
							this.dyadroollist = data.Response.dyadroollist
						}
						this.refreshing = false;
						uni.stopPullDownRefresh();
					}
				});
			},
			loadAllGoodsCategory: function() {
				uni.showLoading({
					title: '加载中...'
				})
				api.get({
					url: 'Goods/GetAllGoodsCategoryList',
					data: {},
					success: data => {
						uni.hideLoading()
						console.info('loadAllGoodsCategory:' + JSON.stringify(data))
						if (data.ErrCode == 0) {
							// this.selectList = data.Response
							if (this.selectList.length <= 0 || this.selectList[0].id != data.Response[0]
								.id) {
								for (var i = 0; i < data.Response.length; i++) {
									this.selectList.push(data.Response[i])
									if (this.selectList.length == 4) break;
								}
							}
						}
					}

				});
			},
			/**
			 * 跳转到商品详情
			 */
			toGoodsDetail: function(item) {
				console.log(item);
				uni.navigateTo({
					url: "/module/good/goodsDetails/goodsDetails?id=" + item.id
				});
			},
			/**
			 * 跳转更多
			 */
			toMore: function() {
				console.log('toMore')
				uni.switchTab({
					url: '/pages/allCategories/allCategories'
				});
			},
			/**
			 * 跳转到关键词
			 */
			toSearch: function(search) {
				// uni.switchTab({
				// 	url:'/pages/allCategories/allCategories'
				// });
				console.log(search)
				uni.navigateTo({
					url: '/module/good/goodsList/goodsList?key=' + search
				});
				// uni.navigateTo({
				// 	url:'/pages/goodsSearchResult/goodsSearchResult?key='+search
				// }); 
			},
			/**
			 * 跳转到关键词
			 */
			toSearchList: function(item) {
				// uni.switchTab({
				// 	url:'/pages/allCategories/allCategories'
				// });
				console.log(item)
				uni.navigateTo({
					url: '/module/good/goodsList/goodsList?category_id=' + item.id
				});
				// uni.navigateTo({
				// 	url:'/pages/goodsSearchResult/goodsSearchResult?key='+search
				// }); 
			},
			/**
			 * 跳转到新品推荐更多
			 */
			toRedGoodsListMore: function() {
				console.log('toRedGoodsListMore')
				uni.navigateTo({
					url: '/module/good/hotCommodity/hotCommodity'
				});
			},
			/**
			 * 跳转到优惠推荐更多
			 */
			toActiveGoodsListMore: function() {
				console.log('toactiveGoodsListMore')
				uni.navigateTo({
					url: '/module/good/activegoodslist/activegoodslist'
				});
			},
			/**
			 * 跳转到热卖推荐更多
			 */
			toHotGoodsListMore: function() {
				console.log('tohotGoodsListMore')
				uni.navigateTo({
					url: '/module/good/hotgoodslist/hotgoodslist'
				});
			},
			/**
			 * 跳转到轮播指定地址
			 */
			toBunnerUrl: function(item) {
				console.log('toBunnerUrl' + JSON.stringify(item))
				// location.href = 'https:/www.baidu.com'
				uni.navigateTo({
					url: '/module/my/myBanner/myBanner/?key=' + item.url
				});
			},
			toInputResult: function() {
				uni.navigateTo({
					url: "/module/good/goodsSearchResult/goodsSearchResult?key=" + this.searchKey
				})
			},
			toInputSearch: function() {
				uni.navigateTo({
					url: "/module/good/goodsSearch/goodsSearch"
				})
			}


		}
	}
</script>

<style lang="scss">
	page {
		background: #f7f7f7;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
		color: #999999;
		font-size: 32rpx;
	}

	.scroll-view-item_H {
		display: inline-block;
		width: 20%;
		height: 100rpx;
		position: relative;
		padding: 10px 0;
		margin: 0 auto;
		text-align: center;

		.activite {
			width: 100%;
			color: #000;
			font-weight: 600;
			margin: 10rpx auto;

		}

		.activite:before {
			display: block;
			content: "";
			margin: 0 auto;
			background: #ea562b;
			height: 10rpx;
			width: 40rpx;
			border-radius: 15rpx;



		}
	}

	.content {
		margin-top: 40rpx;
	}

	.index_2 {
		.phcolor {
			color: #aaa;
			font-size: 24rpx;
			letter-spacing: 6rpx;
		}

		.recommend-info {
			padding: 20rpx;

			.goods-name {
				font-size: 32rpx;
				padding: 20rpx;
			}
		}

		.recommend-bto {
			padding: 20rpx;

			.text-price {
				font-size: 48rpx !important;
				color: #ea562b;
			}

			.text-mkprice {
				color: #acacac !important;
				text-decoration: line-through;
				font-size: 28rpx !important;
				margin-left: 20rpx;
			}

			.text-mkprice:before {
				content: "￥";
			}

			.text-salenum {
				color: #222 !important;
				font-size: 28rpx !important;
				margin-left: 40rpx;
			}
		}

		.recommend-bto>view {

			border-radius: 50%;
			/*background-color: #fde1e1;*/
		}

		.recommend-bto>view>text {
			color: #ea562b;
			font-size: 20upx;
		}

		.recommend-bto>text {
			font-size: 48upx;
			color: #ea562b;
		}

		.recommend-img1 {
			width: 100%;
			height: 630upx;
			overflow: hidden;
			border-radius: 15rpx 15rpx 0 0;
		}

		.recommend-img2 {
			width: 160upx;
			height: 160upx;
		}

		.recommend-item>text {
			font-size: 22upx;
			color: #666666;
			margin: 12upx 0;
		}

		.recommend-item {
			margin-bottom: 24upx;
		}

		.recommend {
			margin-top: 20upx;
			background-color: #fff;
			padding: 0 30upx 0 30upx;
			box-sizing: border-box;
		}

		.recommend-title {
			height: 68upx;
		}

		.recommend-title>view>text {
			font-size: 24upx;
			color: #999999;
		}

		.recommend-title>text {
			font-size: 30upx;
			color: #333333;
			font-weight: 500;
		}

		.index-nav-item image {
			width: 80upx;
			height: 80upx;
			border-radius: 40upx;
		}

		.index-nav-item text {
			font-size: 24upx;
			color: #333333;
			margin-top: 10upx;
		}

		.index-nav {
			width: 100%;
			height: 160upx;
			background-color: #fff;
		}

		.search input {

			height: 40upx;
			background-color: #ffffff;
			border-radius: 20upx;
			padding-left: 30upx;
			box-sizing: border-box;
		}

		.search {
			height: 120upx;
			background: #f7f7f7;
			width: 100%;
		}

		.search>.search_btn {
			color: #000;
			right: 40upx;
			position: absolute;
		}

		.top-search {
			width: 95%;
			height: 80upx;
			background-color: #ffffff;
			border-radius: 10upx;
			padding: 0 26upx;
			box-sizing: border-box;

		}

		.cuIcon-search {
			position: absolute;
		}

		.top-search {
			font-size: 24upx;
		}


		.discount {
			color: #888888;
			margin: 30rpx 20rpx 20rpx;
			height: 50rpx;

			.discount_top {
				display: flex;

				.discount_top_left {
					width: 25%;
				}

				.discount_top_right {
					width: 75%;
					display: flex;
					justify-content: space-between;
				}
			}

		}

		.discountlist {
			display: flex;
			flex-flow: row wrap;
			justify-content: space-around;
			margin: 0rpx 40rpx;

			.discountitem {
				background-color: #fff;
				border-radius: 15rpx;
				padding: 0rpx;
				width: 100%;
				margin: 20rpx 0;
			}

		}
	}
</style>